﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using Microsoft.ApplicationBlocks.Data;
using System.Text;
using CWCare.Modules.cwcProvider.Components;
using System.Data.Linq;

namespace CWCare.Modules.cwcProvider
{
    public partial class MappingProvider : PortalModuleBase
    {
        private const string Certification_Provider = "Certification_Provider";
        private const string Hospital_Provider = "Hospital_Provider";
        private const string Location_Provider = "Location_Provider";
        private const string Specailty_Provider = "Specailty_Provider";
        private const string SpokenLanguage_Provider = "SpokenLanguage_Provider";
        private const string Insurance_Provider = "Insurance_Provider";

        //private const string MasterId = "MasterId";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ddlTables.Items.Add(Certification_Provider);
                ddlTables.Items.Add(Hospital_Provider);
                ddlTables.Items.Add(Location_Provider);
                ddlTables.Items.Add(Specailty_Provider);
                ddlTables.Items.Add(SpokenLanguage_Provider);
                ddlTables.Items.Add(Insurance_Provider);
                this.SetLisrProviderId();
                SetMapping();
                //if (Session[MasterId] != null)
                //    Session.Remove(MasterId);
            }
        }

        private void SetLisrLocationId(DropDownList dll)
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from a in context.GetTable<cwc_Location>()
                        select new ListItem(a.address, a.addressID.ToString()));

            dll.DataSource = info;
            dll.DataTextField = "text";
            dll.DataValueField = "value";
            dll.DataBind();
        }

        private void SetLisrCertificationId(DropDownList dll)
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from a in context.GetTable<cwc_Certification>()
                        select new ListItem(a.certification, a.certificationID.ToString()));

            dll.DataSource = info;
            dll.DataTextField = "text";
            dll.DataValueField = "value";
            dll.DataBind();
        }

        private void SetLisrHospitalId(DropDownList dll)
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from a in context.GetTable<cwc_Hospital>()
                        select new ListItem(a.hospitalName, a.hospitalID.ToString()));

            dll.DataSource = info;
            dll.DataBind();
        }

        private void SetLisrSpecialtyId(DropDownList dll)
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from a in context.GetTable<cwc_Specialty>()
                        select new ListItem(a.specialty, a.specialtyID.ToString()));

            dll.DataSource = info;
            dll.DataTextField = "text";
            dll.DataValueField = "value";
            dll.DataBind();
        }

        private void SetLisrSpokenLanguageId(DropDownList dll)
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from a in context.GetTable<cwc_SpokenLanguage>()
                        select new ListItem(a.spokenLanguage, a.languageID.ToString()));

            dll.DataSource = info;
            dll.DataTextField = "text";
            dll.DataValueField = "value";
            dll.DataBind();
        }


        private void SetLisrInsuranceId(DropDownList dll)
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from a in context.GetTable<cwc_Insurance>()
                        select new ListItem(a.company, a.insuranceID.ToString()));

            dll.DataSource = info;
            dll.DataTextField = "text";
            dll.DataValueField = "value";
            dll.DataBind();
        }

        private void SetLisrProviderId()
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from pro in context.GetTable<cwc_Provider>()
                        select new ListItem(pro.fName + " " + pro.lName, pro.providerID.ToString()));

            ddlProviderId.DataSource = info;
            ddlProviderId.DataTextField = "text";
            ddlProviderId.DataValueField = "value";
            ddlProviderId.DataBind();
        }

        private void SetMapping()
        {
            switch (ddlTables.SelectedValue)
            {
                case Certification_Provider:
                    this.SetLisrCertificationId(this.ddlMapping);
                    lblMappingName.Text = "Certification";
                    break;
                case Hospital_Provider:
                    this.SetLisrHospitalId(this.ddlMapping);
                    lblMappingName.Text = "Hospital";
                    break;
                case Specailty_Provider:
                    this.SetLisrSpecialtyId(this.ddlMapping);
                    lblMappingName.Text = "Specialty";
                    break;
                case Location_Provider:
                    this.SetLisrLocationId(this.ddlMapping);
                    lblMappingName.Text = "Location";
                    break;
                case SpokenLanguage_Provider:
                    this.SetLisrSpokenLanguageId(this.ddlMapping);
                    lblMappingName.Text = "Spoken Language";
                    break;
                case Insurance_Provider:
                    this.SetLisrInsuranceId(this.ddlMapping);
                    lblMappingName.Text = "Insurance";
                    break;
            }
        }

        private void InsertMappingProvider()
        {
            cwcareDataContext context = new cwcareDataContext();
            bool flgInsert = false;
            switch (ddlTables.SelectedValue)
            {
                case Certification_Provider:
                    cwc_Certification_Provider cert = new cwc_Certification_Provider();
                    cert.certificationID = int.Parse(this.ddlMapping.SelectedValue);
                    cert.providerID = int.Parse(this.ddlProviderId.SelectedValue);
                    if (this.GetCertProvider(ref cert) == false)
                    {
                        context.cwc_Certification_Providers.InsertOnSubmit(cert);
                        flgInsert = true;
                    }
                    break;
                case Hospital_Provider:
                    cwc_Hospital_Provider provider = new cwc_Hospital_Provider();
                    provider.hospitalID = int.Parse(this.ddlMapping.SelectedValue);
                    provider.providerID = int.Parse(this.ddlProviderId.SelectedValue);
                    if (this.GetHospProvider(ref provider) == false)
                    {
                        context.cwc_Hospital_Providers.InsertOnSubmit(provider);
                        flgInsert = true;
                    }
                    break;
                case Specailty_Provider:
                    cwc_Specialty_Provider spec = new cwc_Specialty_Provider();
                    spec.specialtyID = int.Parse(this.ddlMapping.SelectedValue);
                    spec.providerID = int.Parse(this.ddlProviderId.SelectedValue);

                    if (this.GetSpecProvider(ref spec) == false)
                    {
                        context.cwc_Specialty_Providers.InsertOnSubmit(spec);
                        flgInsert = true;
                    }
                    break;
                case Location_Provider:
                    cwc_Location_Provider loc = new cwc_Location_Provider();
                    loc.addressID = int.Parse(this.ddlMapping.SelectedValue);
                    loc.providerID = int.Parse(this.ddlProviderId.SelectedValue);

                    if (this.GetLocaProvider(ref loc) == false)
                    {
                        context.cwc_Location_Providers.InsertOnSubmit(loc);
                        flgInsert = true;
                    }
                    break;
                case SpokenLanguage_Provider:
                    cwc_SpokenLanguage_Provider lang = new cwc_SpokenLanguage_Provider();
                    lang.providerID = int.Parse(this.ddlProviderId.SelectedValue);
                    lang.languageID = int.Parse(this.ddlMapping.SelectedValue);

                    if (this.GetSpokProvider(ref lang) == false)
                    {

                        context.cwc_SpokenLanguage_Providers.InsertOnSubmit(lang);
                        flgInsert = true;
                    }
                    break;
                case Insurance_Provider:
                    cwc_Insurance_Provider insu = new cwc_Insurance_Provider();
                    insu.providerID = int.Parse(this.ddlProviderId.SelectedValue);
                    insu.insuranceID = int.Parse(this.ddlMapping.SelectedValue);

                    if (this.GetInsuProvider(ref insu) == false)
                    {

                        context.cwc_Insurance_Providers.InsertOnSubmit(insu);
                        flgInsert = true;
                    }
                    break;
            }
            if (flgInsert == true)
            {
                try
                {
                    context.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                }
            }
        }

        private bool GetCertProvider(ref cwc_Certification_Provider resutl)
        {
            cwcareDataContext context = new cwcareDataContext();

            var info = (from a in context.GetTable<cwc_Certification_Provider>()
                        where a.providerID == int.Parse(this.ddlProviderId.SelectedValue) && a.certificationID == int.Parse(this.ddlMapping.SelectedValue)
                        select a).ToList<cwc_Certification_Provider>();
            if (info.Count > 0)
            {
                resutl = info[0];
                return true;
            }
            return false;
        }

        private bool GetHospProvider(ref cwc_Hospital_Provider resutl)
        {
            cwcareDataContext context = new cwcareDataContext();

            var info = (from a in context.GetTable<cwc_Hospital_Provider>()
                        where a.providerID == int.Parse(this.ddlProviderId.SelectedValue) && a.hospitalID == int.Parse(this.ddlMapping.SelectedValue)
                        select a).ToList<cwc_Hospital_Provider>();
            if (info.Count > 0)
            {
                resutl = info[0];
                return true;

            }
            return false;
        }

        private bool GetSpecProvider(ref cwc_Specialty_Provider resutl)
        {
            cwcareDataContext context = new cwcareDataContext();

            var info = (from a in context.GetTable<cwc_Specialty_Provider>()
                        where a.providerID == int.Parse(this.ddlProviderId.SelectedValue) && a.specialtyID == int.Parse(this.ddlMapping.SelectedValue)
                        select a).ToList<cwc_Specialty_Provider>();
            if (info.Count > 0)
            {
                resutl = info[0];
                return true;
            }
            return false;
        }

        private bool GetLocaProvider(ref cwc_Location_Provider resutl)
        {
            cwcareDataContext context = new cwcareDataContext();

            var info = (from a in context.GetTable<cwc_Location_Provider>()
                        where a.providerID == int.Parse(this.ddlProviderId.SelectedValue) && a.addressID == int.Parse(this.ddlMapping.SelectedValue)
                        select a).ToList<cwc_Location_Provider>();
            if (info.Count > 0)
            {
                resutl = info[0];
                return true;
            }
            return false;
        }

        private bool GetSpokProvider(ref cwc_SpokenLanguage_Provider resutl)
        {
            cwcareDataContext context = new cwcareDataContext();

            var info = (from a in context.GetTable<cwc_SpokenLanguage_Provider>()
                        where a.providerID == int.Parse(this.ddlProviderId.SelectedValue) && a.languageID == int.Parse(this.ddlMapping.SelectedValue)
                        select a).ToList<cwc_SpokenLanguage_Provider>();
            if (info.Count > 0)
            {
                resutl = info[0];
                return true;
            }
            return false;
        }

        private bool GetInsuProvider(ref cwc_Insurance_Provider resutl)
        {
            cwcareDataContext context = new cwcareDataContext();

            var info = (from a in context.GetTable<cwc_Insurance_Provider>()
                        where a.providerID == int.Parse(this.ddlProviderId.SelectedValue) && a.insuranceID == int.Parse(this.ddlMapping.SelectedValue)
                        select a).ToList<cwc_Insurance_Provider>();
            if (info.Count > 0)
            {
                resutl = info[0];
                return true;
            }
            return false;
        }

        private void DeleteMappingProvider()
        {
            cwcareDataContext context = new cwcareDataContext();
            bool flgDelete = false;
            switch (ddlTables.SelectedValue)
            {
                case Certification_Provider:
                    cwc_Certification_Provider cert = new cwc_Certification_Provider();
                    if (this.GetCertProvider(ref cert))
                    {
                        context.cwc_Certification_Providers.Attach(cert);
                        context.cwc_Certification_Providers.DeleteOnSubmit(cert);
                        flgDelete = true;
                    }
                    break;
                case Hospital_Provider:

                    cwc_Hospital_Provider hosp = new cwc_Hospital_Provider(); ;
                    if (this.GetHospProvider(ref hosp))
                    {
                        context.cwc_Hospital_Providers.Attach(hosp);
                        context.cwc_Hospital_Providers.DeleteOnSubmit(hosp);
                        flgDelete = true;
                    }
                    break;
                case Specailty_Provider:
                    cwc_Specialty_Provider spec = new cwc_Specialty_Provider();

                    if (this.GetSpecProvider(ref spec))
                    {
                        context.cwc_Specialty_Providers.Attach(spec);
                        context.cwc_Specialty_Providers.DeleteOnSubmit(spec);
                        flgDelete = true;
                    }
                    break;
                case Location_Provider:
                    cwc_Location_Provider loca = new cwc_Location_Provider();

                    if (this.GetLocaProvider(ref loca))
                    {
                        context.cwc_Location_Providers.Attach(loca);
                        context.cwc_Location_Providers.DeleteOnSubmit(loca);
                        flgDelete = true;
                    }
                    break;
                case SpokenLanguage_Provider:
                    cwc_SpokenLanguage_Provider spok = new cwc_SpokenLanguage_Provider();

                    if (this.GetSpokProvider(ref spok))
                    {
                        context.cwc_SpokenLanguage_Providers.Attach(spok);
                        context.cwc_SpokenLanguage_Providers.DeleteOnSubmit(spok);
                        flgDelete = true;
                    }
                    break;
            }
            if (flgDelete == true)
            {
                try
                {
                    context.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                }
            }
        }



        private int GetIndexItem(DropDownList ddl, string value)
        {
            int index = -1;
            var info = (from a in ddl.Items.Cast<ListItem>()
                        where (a.Value == value || a.Text == value)
                        select a).ToList<ListItem>();
            if (info.Count > 0)
            {
                index = ddl.Items.IndexOf(info[0]);
            }
            return index;
        }


        protected void btnInsert_OnClick(object sender, EventArgs e)
        {
            this.InsertMappingProvider();
            string paraId = string.Empty;
            if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
            {
                paraId = "&pid=" + Request.Params.Get("pid");
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            this.DeleteMappingProvider();

            string paraId = string.Empty;
            if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
            {
                paraId = "&pid=" + Request.Params.Get("pid");
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);

        }

        protected void btnCancel_OnClick(object sender, EventArgs e)
        {
            string paraId = string.Empty;
            if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
            {
                paraId = "&pid=" + Request.Params.Get("pid");
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);
        }


        protected void ddlTables_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetMapping();
        }
    }

}


